package com.situ.blog.servlet;

import com.situ.blog.pojo.Blog;
import com.situ.blog.pojo.BlogType;
import com.situ.blog.pojo.query.BlogTypeQuery;
import com.situ.blog.service.IBlogTypeService;
import com.situ.blog.service.impl.BlogTypeServiceImpl;
import com.situ.blog.util.JSONUtil;
import com.situ.blog.util.MyBeanUtil;
import com.situ.blog.util.PageRusult;
import com.situ.blog.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/type")
public class BlogTypeServlet extends HttpServlet {
    IBlogTypeService blogTypeService = new BlogTypeServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        switch (method){
            case "selectAll":
                selectAll(req,resp);
                break;
            case "selectByPage":
                selectByPage(req,resp);
                break;
            case "add":
                add(req,resp);
                break;
            case "deleteById":
                deleteById(req,resp);
                break;
            case "deleteAll":
                deleteAll(req,resp);
                break;
            case "selectById":
                selectById(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
        }
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogTypeServlet.selectByPage");
        BlogTypeQuery blogTypeQuery = MyBeanUtil.copyToBean(req, BlogTypeQuery.class);//封装好了参数
        //调用函数
        System.out.println("blogTypeQuery"+blogTypeQuery);
        PageRusult<BlogType> pageRusult = blogTypeService.selectByPage(blogTypeQuery);
        System.out.println(pageRusult);
        JSONUtil.toJSON(resp,pageRusult);
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogTypeServlet.selectAll");
        //获取参数
        BlogType blogType = MyBeanUtil.copyToBean(req, BlogType.class);
        System.out.println(blogType);
        List<BlogType> list = blogTypeService.selectAll();
        Result result = new Result(0,"查找成功",list);
        JSONUtil.toJSON(resp,result);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.add");
        //封装一下
        BlogType blogType = MyBeanUtil.copyToBean(req, BlogType.class);
        System.out.println(blogType);
        blogTypeService.add(blogType);
        //返回Ajax数据
        Result result = new Result(0,"添加成功");
        JSONUtil.toJSON(resp,result);
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("BlogServlet.deleteById");
        //接收参数
        String id = req.getParameter("id");
        System.out.println("id:"+id);
        //调用函数
        int count = blogTypeService.deleteById(Integer.parseInt(id));//没有返回值
        Result result = null;
        if (count>0){
            result = new Result(0,"删除成功");
        }else {
            result = new Result(1,"删除失败");
        }
        //固定的三行
        JSONUtil.toJSON(resp,result);
        //*resp.setContentType("text/html;charset=UTF-8");
        //ObjectMapper mapper = new ObjectMapper();
        //mapper.writeValue(resp.getWriter(), result);//*//现在不知道这里要返回什么
    }
    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.deleteAll");
        //传过来的是一个id的数组
        String[] ids = req.getParameterValues("ids[]");
        System.out.println(ids);
        //收到了传过来的id值，然后调用service
        blogTypeService.deleteAll(ids);
        Result result = new Result(0,"删除成功");
        JSONUtil.toJSON(resp,result);
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {

        System.out.println("BlogTypeServlet.selectById");
        String id = req.getParameter("id");
        System.out.println("传过来了吗？："+id);
        BlogType blogType = blogTypeService.selectById(id);

        JSONUtil.toJSON(resp,blogType);

    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogServlet.update");
        BlogType blogType = MyBeanUtil.copyToBean(req, BlogType.class);
        System.out.println("id="+blogType.getId());
        blogTypeService.update(blogType);
        Result result = new Result(0,"修改成功");
        JSONUtil.toJSON(resp,result);
    }
}
